#!/usr/bin/env python
# -*- coding: utf-8 -*-
#######################################################################
# nextShows - GUI launcher
# Copyright (C) 2006-2008 Gilles CHAUVIN <gcnweb@gmail.com>
# $Id: launchGUI 110 2008-03-05 10:41:25Z gcnweb $
#######################################################################
# Coding: UTF-8, 4 spaces indent, LF line terminator
#######################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2  of  the  License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be  useful, but
# WITHOUT  ANY  WARRANTY;  without  even  the   implied   warranty  of
# MERCHANTABILITY or FITNESS FOR A  PARTICULAR  PURPOSE. See  the  GNU
# General Public License for more details.
#
# You should have received a copy of the GNU  General  Public  License
# along with  this  program;  if  not,  write  to  the  Free  Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#######################################################################

from cgui.NextShowsConfig import NextShowsConfig
import libs.tools as tools
import os, sys, time

try:
    from PyQt4.Qt import *
except:
    os.system('kdialog --title "Error" --error "The nextShows configuration GUI requires PyQt4!\nPlease install a recent version and retry." &')
    sys.exit()


# Check PyQt version
if PYQT_VERSION < 0x040300:
    message = "Your PyQt version is too old (v%s)!\nYou need PyQt v4.3.0 or later." % PYQT_VERSION_STR
    os.system('kdialog --title "Error" --error "'+message+'" &')
    sys.exit()
# Check Qt version
if QT_VERSION < 0x040300:
    message = "Your Qt version is too old (v%s)!\nYou need Qt v4.3.0 or later." % QT_VERSION_STR
    os.system('kdialog --title "Error" --error "'+message+'" &')
    sys.exit()


class Splash:
    def __init__(self):
        self.splash      = QSplashScreen()
        self.pixLogo     = QPixmap(":/images/logos/images/logos/nextShows.png")
        self.pixDesktop  = QPixmap()
        self.captureArea = QRect()
        self.animStep    = 0.0000001

    def init(self):
        ## FIXME: Desktop area screenshot doesn't work properly with multiple-displays
        desktopAGeo = qApp.desktop().availableGeometry( qApp.desktop().primaryScreen() )
        self.captureArea = QRect( ( desktopAGeo.width()  - self.pixLogo.width()  ) / 2,
                                         ( desktopAGeo.height() - self.pixLogo.height() ) / 2,
                                           self.pixLogo.width(),
                                           self.pixLogo.height() )
        self.pixDesktop  = QPixmap().grabWindow( QApplication.desktop().winId(),
                                                       self.captureArea.x(),
                                                       self.captureArea.y(),
                                                       self.captureArea.width(),
                                                       self.captureArea.height() )
    def finish(self, window):
        self.splash.finish(window)

    def fadeIn(self):
        for i in range(0, 101):
            self._showImage( float(i) / 100 )
            time.sleep( self.animStep )

    def fadeOut(self):
        for i in range(0, 101):
            self._showImage( float(100-i) / 100 )
            time.sleep( self.animStep )

    def _showImage(self, opacity):
        painter   = QPainter()
        pixSplash = QPixmap( self.captureArea.width(), self.captureArea.height() )

        painter.begin( pixSplash )
        painter.drawPixmap( 0, 0, self.pixDesktop )
        painter.setOpacity( opacity )
        painter.drawPixmap( 0, 0, self.pixLogo )
        painter.end()

        self.splash.setPixmap( pixSplash )
        self.splash.show()


def main():
    # Init qApp
    app=QApplication(sys.argv)
    # Splash "in" / init
    splash = Splash()
    splash.init()
    splash.fadeIn()
    # Load main window
    window=NextShowsConfig()
    # Splash "out"
    splash.fadeOut()
    # Show main window
    window.show()
    splash.finish(window)
    # Entering Event Loop
    return app.exec_()


if __name__ == "__main__":
    RETVAL=main()
    print "Exit: %d" % RETVAL   # This is to make sure SK catches when GUI was closed
                                # using commandOutput()
    sys.exit(RETVAL)
